package com.saishi.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import com.saishi.dao.impl.tkxtDaoImpl;
import com.saishi.dao.tkxtDao;
import com.saishi.dao.impl.ClassesDaoImpl;
import com.saishi.entity.Question;
import org.apache.poi.hssf.usermodel.HSSFHeader;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.*;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/tkxt/*")
public class tkxtServlet extends BaseServlet {
    tkxtDao dao=new tkxtDaoImpl();

    public void selectLike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String name= request.getParameter("questionname");
        List list = dao.selectLike(name);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/tkxtFind.jsp").forward(request,response);
    }

    public void selectdx(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String type= request.getParameter("type");
        int tt=Integer.parseInt(type);
        List list =dao.selectdx(tt);

        request.setAttribute("list",list);
        request.getRequestDispatcher("/tkxtFind.jsp").forward(request,response);
    }

    public void selectduox(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String type= request.getParameter("type");
        int tt=Integer.parseInt(type);
        List list = dao.selectdx(tt);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/tkxtFind.jsp").forward(request,response);
    }

    public void selecttk(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String type= request.getParameter("type");
        int tt=Integer.parseInt(type);
        List list = dao.selectdx(tt);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/tkxtFind.jsp").forward(request,response);
    }
    ArrayList llll = new ArrayList();
    public void listid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {


        String iid=request.getParameter("id");
        int id=Integer.parseInt(iid);
        HttpSession session= request.getSession();
        //      判断list里是否有该id
        while(!llll.contains(id)){
            System.out.println("没有");
            llll.add(id);
        }
        System.out.println(llll);
        session.setAttribute("listid",llll);
//        request.getRequestDispatcher("/tkxt.jsp").forward(request,response);
        response.sendRedirect("/tkxt.jsp");
    }


    public void addId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        HttpSession session=request.getSession();
        ArrayList list= (ArrayList) session.getAttribute("listid");
        System.out.println("list="+list);
        String name= StringUtils.join(list,",");
        System.out.println("name="+name);

        //examid
        int id= (int) session.getAttribute("examid");
        System.out.println("examid="+id);

        //通过id获取到当前的题
        String ti=dao.selectById(id);
        System.out.println("ti="+ti);
        if(ti!=null&&ti.length()>=1){
            String xin=ti+","+name;
            System.out.println("xin="+xin);
            //调佣修改方法
            dao.addId(id,xin);
        }else{

            //把数据库取出的和新加的合并
            dao.addId(id,name);
        }


        //把list改成空
        llll=null;
        session.setAttribute("listid",llll);
        response.sendRedirect("/sjbx/seletm");
    }



}
